<?php
	class CmsModel extends CI_Model {
		// 表名
		static $settingTable = "school_cms_setting";
		static $articleTable = "school_cms_article";
		static $categoryTable = "school_cms_category";
		static $sliderTable = "school_cms_slider";
		static $linkTable = "school_cms_link";

	    public function __construct() {
	        parent::__construct();
	    }

	    // 获取栏目信息
	    public function get_category_info($categoryId){
	    	$where = "`id`='{$categoryId}'";
	    	$categoryInfo = $this->db->where($where)->get(self::$categoryTable)->row_array();
	    	return $categoryInfo;
	    }

	    // 获取栏目列表
	    public function get_category_list($params = ""){
	    	// 获取一级栏目
	    	$where = " 1 AND 1";
	    	// 过滤参数
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	// 栏目类型
	    	if(isset($params['cateType'])) $where .= " AND `category_type`='{$params['cateType']}'";
	    	// 栏目推荐
	    	if(isset($params['cateRecommend'])) $where .= " AND `category_recommend`='{$params['cateRecommend']}'";
	    	// 获取子分类
	    	if(isset($params['parentId'])) $where .= " AND `parent_id`='{$params['parentId']}'";

	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
	    	// 排序
	    	$orderBy = "`category_sort` DESC";
	    	$categoryList = array();
	    	// 获取一级栏目
	    	$firstCategory = $this->db->where($where)->order_by($orderBy)->get(self::$categoryTable)->result_array();
	    	foreach($firstCategory as $category) {
	    		$category['parentName'] = "一级栏目";
	    		$categoryList[$category['id']] = $category;
			
	    		// 获取二级栏目		
	    		if(isset($params['getChild'])) {
		    		$where = "`parent_id`='{$category['id']}' AND `is_delete` = 0";
		    		$childCategory = $this->db->where($where)->order_by($orderBy)->get(self::$categoryTable)->result_array();
		    		foreach($childCategory as $child) {
		    			$child['parentName'] = $category['category_name'];
		    			$categoryList[$child['id']] = $child;
		    		}
		    	}
	    	}
	    	return $categoryList;
	    }

	    // 获取文章列表
	    public function get_article_list($params = ""){
	    	// 查询
	    	$where = "1 AND 1";
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	if(isset($params['categoryId'])) $where .= " AND `category_id` IN ('{$params['categoryId']}')";
	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
	    	$this->db->where($where);
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	// 是否只获取条数
	    	if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$articleTable);
	    	return $this->db->order_by("`article_sort` DESC, `id` DESC")->get(self::$articleTable)->result_array();
	    }
	    // 获取文章信息
	    public function get_article_info($articleId){
	    	$where = "`id`='{$articleId}'";
	    	$categoryInfo = $this->db->where($where)->get(self::$articleTable)->row_array();
	    	return $categoryInfo;
	    }

	    // 获取上一条文章
	    public function get_article_next($params){
	    	// 查询
	    	$where = "1 AND 1";
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	if(isset($params['categoryId'])) $where .= " AND `category_id`='{$params['categoryId']}'";
	    	if(isset($params['getNext'])) $where .= " AND `id` > '{$params['getNext']}'";
	    	if(isset($params['getPrev'])) $where .= " AND `id` < '{$params['getPrev']}'";
	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
	    	$this->db->where($where);
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	return $this->db->order_by("`article_sort` DESC, `id` DESC")->get(self::$articleTable)->row_array();
	    }

	    // 获取幻灯片列表
	    public function get_slider_list($params = ""){
	    	// 查询
	    	$where = "1 AND 1";
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
	    	$this->db->where($where);
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	// 是否只获取条数
	    	if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$sliderTable);
	    	return $this->db->order_by("`slider_sort` DESC")->get(self::$sliderTable)->result_array();
	    }

	    // 获取友情链接列表
	    public function get_link_list($params = ""){
	    	// 查询
	    	$where = "1 AND 1";
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
	    	$this->db->where($where);
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	// 是否只获取条数
	    	if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$linkTable);
	    	return $this->db->order_by("`link_sort` DESC")->get(self::$linkTable)->result_array();
	    }

	    // 获取官网设置
	    public function get_cms_setting($schoolId, $campusId) {
	    	$where = "`school_id`='{$schoolId}' AND `campus_id`='{$campusId}'";
	    	$settingInfo = $this->db->where($where)->get(self::$settingTable)->row_array();
	    	return $settingInfo;
	    }
	    // 根据token，获取官网设置
	    public function get_cms_setting_by_token($token) {
	    	$where = "`cms_token`='{$token}'";
	    	$settingInfo = $this->db->where($where)->get(self::$settingTable)->row_array();
	    	return $settingInfo;
	    }
	}